package main

import (
	"fmt"
	"log"
	"net/http"
	"sync"
	"time"
)

func main() {
	var wg sync.WaitGroup
	wg.Add(1)
	go task1(&wg)
	wg.Wait()

	wg.Add(2)
	go task2(&wg)
	go task3(&wg)
	wg.Wait()
	wg.Add(2)
	go task4(&wg)
	go task5(&wg)
	wg.Wait()
}

func task1(wg *sync.WaitGroup) {
	defer wg.Done()
	resp, err := http.Get("http://localhost:8080/")
	if err != nil {
		log.Fatalf("%v", err)
	}

	fmt.Printf("task 1\n %v\n", resp)
}

func task2(wg *sync.WaitGroup) {
	defer wg.Done()
	count := 0
	for i := 0; i < 1_000_000_000; i++ {
		count += i
	}
	fmt.Println("task 2")
}

func task3(wg *sync.WaitGroup) {
	defer wg.Done()
	time.Sleep(time.Millisecond * 10)
	fmt.Println("task 3")
}

func task4(wg *sync.WaitGroup) {
	defer wg.Done()
	time.Sleep(time.Millisecond * 500)
	fmt.Println("task4 done")
}

func task5(wg *sync.WaitGroup) {
	defer wg.Done()
	time.Sleep(time.Millisecond * 8)
	fmt.Println("task5 done")
}

//  GOMAXPROCS=1 GOGC=1 GODEBUG=schetrace=200,scheddetail=1 ./exec
